package zxj.spay.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import zxj.spay.anotation.NoAccess;
import zxj.spay.base.resource.SessionName;
import zxj.spay.entity.User;

/**
 * 权限拦截器
 * @author 朱小杰
 * 日期:2016-05-08
 */
public class AccessFilter implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HandlerMethod hm = (HandlerMethod) handler;
		Method method = hm.getMethod();
		NoAccess access = method.getAnnotation(NoAccess.class);
		if(null != access){
			return true;
		}else{
			User user = (User) request.getSession().getAttribute(SessionName.USER);
			if(null != user){
				return true;
			}else{
				response.sendRedirect(request.getContextPath() + "/");
				return false;
			}
		}
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}

}
